package com.example.zrq1010110087.graceplayer.main;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.telecom.TelecomManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.example.zrq1010110087.graceplayer.data.MediaPlayerUser;
import com.example.zrq1010110087.graceplayer.data.Sdcard_MusicList;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by zrq1010110087 on 2017/10/19.
 */

public class MusicService extends Service {
    @Nullable

    private MediaPlayerUser mediaPlayerUser;



    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayerUser=new MediaPlayerUser(Sdcard_MusicList.getMusicArrayList(this),this);
        TelephonyManager telephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new MyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);
    }

    @Override
    public void onDestroy() {
        mediaPlayerUser.releaseMediaPlayer();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
        int status=intent.getIntExtra("status",0);
        int currentTime=intent.getIntExtra("currentTime",1);
        switch (status) {
            case MediaPlayerUser.MUSIC_COMMAND_PALY: mediaPlayerUser.play_pause_resume();break;
            case MediaPlayerUser.MUSIC_COMMAND_STOP:mediaPlayerUser.stop(); break;
            case  MediaPlayerUser.MUSIC_COMMAND_PREVIOUS:mediaPlayerUser.moveToPrevious();break;
            case  MediaPlayerUser.MUSIC_COMMAND_NEXT:mediaPlayerUser.moveToNext();break;
            case MediaPlayerUser.MUSIC_COMMAND_ONCLICKITEM_NUMBER: int number=intent.getIntExtra("number",0);
                                                                          mediaPlayerUser.onClickListItem(number);break;
            case MediaPlayerUser.MUSIC_COMMAND_PLAYMODEL: int playModel=intent.getIntExtra("playModel",0);mediaPlayerUser.setPlayModel(playModel);break;
            case MediaPlayerUser.MUSIC_COMMAND_COMPLETED:break;
            case MediaPlayerUser.MUSIC_COMMAND_SEEKTO:mediaPlayerUser.seekTo(currentTime);break;
        }
         if(status!=MediaPlayerUser.MUSIC_COMMAND_PLAYMODEL) {
             Intent i = new Intent("android.intent.action.Music_StatusChangedReceiver");
             int musicStatus = mediaPlayerUser.getMusicStatus();
             i.putExtra("status", musicStatus);
             if (musicStatus != mediaPlayerUser.MUSIC_STATUS_STOP && musicStatus != mediaPlayerUser.MUSIC_STATUS_ERROR) {
                 i.putExtra("musicName", mediaPlayerUser.getCurrentMusicName());
                 i.putExtra("musicArtist", mediaPlayerUser.getCurrentMusicArtist());
                 i.putExtra("musicDuration", mediaPlayerUser.getCurrentMusicDuration());
                 i.putExtra("currentTime", mediaPlayerUser.getCurrentTime());
             }
             sendBroadcast(i);
         }
        } catch (IOException e) {
        e.printStackTrace();
    }
    return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
//手机拨打监听器
    private class MyPhoneStateListener  extends PhoneStateListener{

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            boolean phone_ing=false;
            switch (state){
                case TelephonyManager.CALL_STATE_RINGING:
                    if(mediaPlayerUser.getMusicStatus()==mediaPlayerUser.MUSIC_STATUS_STARTED){
                        mediaPlayerUser.pause();
                        phone_ing=true;
                    }break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if(phone_ing==true) {
                        mediaPlayerUser.resume();
                        phone_ing=false;
                    }
                    break;
            }

        }
    }




}
